package com.cennavi.mineplay.mineplay_data.model;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.mineplay.mineplay_data.init.InitTask;
import com.cennavi.mineplay.mineplay_data.model.transform.inter.Transform;

public class HttpTaskAssember {

	private JSONObject json;
	private String destDBJdbc, dbDriverClass, username, password,
			destTableNames;

	public HttpTaskAssember(JSONObject json, String destDBJdbc,
			String dbDriverClass, String username, String password,
			String destTableNames) {
		this.json = json;
		this.destDBJdbc = destDBJdbc;
		this.dbDriverClass = dbDriverClass;
		this.username = username;
		this.password = password;
		this.destTableNames = destTableNames;
	}
	
	public HttpTask assember(){
		HttpConnectionInfo httpinfo = new HttpConnectionInfo();
		
		httpinfo.setCompany_id(json.getIntValue("company_id"));
		
		httpinfo.setFrequency(json.getIntValue("frequency"));
		
		String trans = json.getString("trans");
		
		String[] splitsTrans = trans.split(",");

		List<Transform> transList = new ArrayList<Transform>();
		
		for(String key : splitsTrans){
			String className = InitTask.transList.get(key);
			
			try {
				Transform tran = (Transform) Class.forName(className).newInstance();
				
				transList.add(tran);
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		HttpTask task = new HttpTask(httpinfo,transList, destDBJdbc, dbDriverClass, username, password,destTableNames);
		
		return task;
	}
	
}
